package com.service.impl;

import com.common.exception.db.DeleteException;
import com.common.exception.db.UpdateException;
import com.common.paging.PagingData;
import com.dao.BillDao;
import com.entity.*;
import com.service.BillService;
import com.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/11/30 0030.
 */
@Service
public class BillServiceImpl implements BillService {
    @Autowired
    private BillDao billDao;

    @Override
    public int insertBill(Integer userId, BigDecimal money, Integer types, String description) {
        String uuid = UUIDUtil.get32UUID();
        return billDao.insertBill(userId, uuid, money, types, description);
    }

    @Override
    @Transactional
    public Map findMoneyAndBill(Integer userId, Integer limit) {
        BigDecimal money = billDao.findUserMoney(userId);
        List<UserFreezeMoneyInfo> list = billDao.findFreezeMoneyTime(userId);
        if(list.size() > 0){
            for(UserFreezeMoneyInfo userFreezeMoneyInfo:list){
                int success =billDao.deleteFreezeMoney(userFreezeMoneyInfo.getId());
                    if(success > 0){
                        success = billDao.updateUserMoney(userFreezeMoneyInfo.getUserId(),userFreezeMoneyInfo.getFreezeMoney());
                        if(success <= 0){
                            throw new DeleteException("修改金额失败");
                        }
                    }else{
                        throw new DeleteException("删除冻结金额失败");
                    }
            }
        }
        BigDecimal freezeMoney = billDao.freezeMoney(userId);
        List<BillInfo> list1 = billDao.findBill1(userId, limit);
        List<BillInfo> list2 = billDao.findBill2(userId, limit);
        Map map = new HashMap();
        map.put("money", money);
        map.put("freezeMoney", freezeMoney);
        map.put("list1", list1);
        map.put("list2", list2);
        return map;
    }

    @Override
    public List<BannerInfo> findBanner(Integer type) {
        return billDao.findBanner(type);
    }

    @Override
    public List<AnnouncementInfo> findAnnouncement(Integer type) {
        return billDao.findAnnouncement(type);
    }

    @Override
    public int insertUserP(Integer userId, Integer pid) {
        int count = billDao.findUserP(userId, pid);
        if (count == 1) {
            return billDao.updateUserP(userId, pid);
        }
        return count;
    }

    @Override
    public PagingData findCerTeacherValueList(Integer page, Integer length, String search) {
        List<TeacherValueInfo> list = billDao.findCerTeacherValueList(page, length, search);
        int count = billDao.findCerTeacherValueListCount(search);
        PagingData pagingData = new PagingData();
        pagingData.setData(list);
        pagingData.setRecordsTotal(count);
        return pagingData;
    }

    @Override
    public int editTeacherValue(Integer id, Integer teacherValue) {
        return billDao.editTeacherValue(id, teacherValue);
    }

    @Override
    public List findRigLoginCount() {
        List list = new ArrayList();
        for (int i = 1; i <= 20; i++) {
            Map map = new HashMap();
            int num = billDao.findRigLoginCount(i);
            map.put("id", i);
            map.put("num", num);
            list.add(map);
        }
        return list;
    }

    @Override
    public PagingData findRefundRecord(Integer page, Integer length) {
        List<RefundRecordInfo> list = billDao.findRefundRecord(page, length);
        int count = billDao.findRefundRecordCount();
        PagingData pagingData = new PagingData();
        pagingData.setData(list);
        pagingData.setRecordsTotal(count);
        return pagingData;
    }

    @Override
    @Transactional
    public int updateRefundRecord(Integer refundId, Integer recordId, Integer type) {
        int edit = billDao.updateRefundRecord1(recordId, type);
        if (edit > 0) {
            edit = billDao.updateRefundRecord2(refundId, type);
            if (edit > 0) {
                return edit;
            } else {
                throw new UpdateException("修改失败");
            }
        } else {
            throw new UpdateException("修改失败");
        }
    }

    @Override
    public int findOpenIdCon(String openId) {
        return billDao.findOpenIdCon(openId);
    }

    @Override
    public int findordersum(Integer userId, Integer type) {
            return billDao.findordersum(userId,type);
    }
}
